import redis
import time

rdb = redis.Redis('127.0.0.1', 6379, 0)

fake_chat_dict = {
    0: '十全十美！',
    1: '一把钢枪交给我！',
    2: '二话不说为祖国！',
    3: '三山五岳任我走。',
    4: '四海为家',
    5: '五福同寿',
    6: '六六大顺！',
    7: '97香港回归。',
    8: '零八奥运。',
    9: '九九归一。',
}
N = 30


def text_generation_zh(xinput):
    xlen = len(xinput)
    xoutput = f'{xinput}您说了{xlen}个字符。（{xinput[:10]}……）'
    for i in range(N):
        time.sleep(0.05)  # for simulation
        offset = (xlen + i) % 10
        if not offset:
            xoutput += '【OK】'
        xoutput += fake_chat_dict[offset]
        yield xoutput


print('---------------READY--------------------')
while True:
    xuuid = rdb.rpop('queue')
    if xuuid is None:
        time.sleep(0.001)  # 重要
        continue

    # 用uuid从hash得到输入
    xinput = rdb.hget('uuid2input', xuuid)
    if xinput is None:
        time.sleep(0.001)  # 重要
        continue
    xinput = xinput.decode('utf8')
    print('input:', xinput)
    # 用uuid从hash得到username
    xusername = rdb.hget('uuid2username', xuuid)
    if xusername is None:
        time.sleep(0.001)  # 重要
        continue
    xusername = xusername.decode('utf8')
    print('username:', xusername)

    # 输入=>输出
    generator = text_generation_zh(xinput)
    xoutput = ''
    for xoutput in generator:
        xoutput = xoutput.encode('utf8')
        rdb.set('chat_cache_' + xusername, xoutput)

    # 把输出放入hash
    rdb.hset('uuid2output', xuuid, xoutput)
